﻿#include "heroplane.h"
#include "config.h"

HeroPlane::HeroPlane()
{
    plane.load(":/res/plane.png");
    postion_x =(GAME_WIDTH*0.5 - plane.width()*0.5);
    postion_y =(GAME_HEIGHT - plane.height() - 100);
    planeRect.setWidth(plane.width());
    planeRect.setHeight(plane.height());
    planeRect.moveTo(getPostionX(),getPostionY());

    bulletCount = 0;
}

void HeroPlane::updatePostion(int x, int y)
{
    if(x<=0){
        x = 0;
    }

    if(x>= GAME_WIDTH - planeRect.width()){
        x = GAME_WIDTH - planeRect.width();
    }

    if(y<=0){
        y = 0;
    }

    if(y>= GAME_HEIGHT - planeRect.height()){
        y = GAME_HEIGHT - planeRect.height();
    }


    postion_x = x;
    postion_y = y;
    planeRect.moveTo(getPostionX(),getPostionY());

}

void HeroPlane::shoot()
{
    bulletCount++;

    if(bulletCount<BULLET_INTERVAL){
        return;
    }

    bulletCount = 0;

    for(int i= 0;i<BULLET_NUM;i++){
        if(bulletsOne[i].getIsFree()){
            bulletsOne[i].setIsFree(false);
            bulletsOne[i].setPostionX(postion_x + planeRect.width() * 0.5);
            bulletsOne[i].setPostionY(postion_y);
            break;
        }
    }
}

int HeroPlane::getPostionX() const
{
    return postion_x;
}

int HeroPlane::getPostionY() const
{
    return postion_y;
}

QPixmap HeroPlane::getPlane() const
{
    return plane;
}

QRect HeroPlane::getPlaneRect() const
{
    return planeRect;
}

